package com.lgq.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.lgq.entity.VisitNum;
import com.lgq.utils.HibernateSessionFactory;

/**
 *@author  刘光强
 *@date    2012-4-7 上午9:36:38
 *@version 1.0
 **/
public class VisitNumDao {
	public boolean addVisitNum(VisitNum visitNum){
		Session session=HibernateSessionFactory.getSession();
		Transaction transaction=session.beginTransaction();
		try {
			session.save(visitNum);
			transaction.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			transaction.rollback();
			return false;
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return true;
	}
	
	public VisitNum getVisitNum(){
		Session session=HibernateSessionFactory.getSession();
		Query query=session.createQuery("from VisitNum u where u.id= 1");
		List<VisitNum> list=query.list();
		VisitNum visitNum=null;
		if (!list.isEmpty()) {
			visitNum=list.get(0);	
		}
		HibernateSessionFactory.closeSession();
		return visitNum;
	}
	
	public boolean updateVisitNum(VisitNum visitNum){
		Session session=HibernateSessionFactory.getSession();
		Transaction transaction=session.beginTransaction();
		try {
			session.update(visitNum);
			transaction.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			transaction.rollback();
			return false;
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return true;
	}
}
